package com.controller.parents;

import com.bean.entity.*;
import com.bean.pojo.Dto;
import com.bean.pojo.PageAssistant;
import com.bean.util.DtoUtil;
import com.bean.vo.*;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("parents")
public class ParentsController {

    @Autowired
    private ParentsInfoService parentsInfoService;
    @Autowired
    private StudentScoreService studentScoreService;
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private PhysicalHealthService physicalHealthService;
    @Autowired
    private StudentCommunicationService studentCommunicationService;
    @Autowired
    private TransAddressService transAddressService;

//    @RequestMapping("login")
//    public Dto parentsLogin(ParentsInfo parentsInfo,HttpSession session){
//        System.out.println(">>>家长登录");
//        System.out.println(parentsInfo);
//
//        try {
//            ParentsInfoVo parentsLoginInfo = parentsInfoService.login(parentsInfo);
//
//            //将家长数据存到session中
//            session.setAttribute("parentsLoginInfo",parentsLoginInfo);
//
//            return DtoUtil.returnSuccess("登录成功！");
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println(e.getMessage());
//            return DtoUtil.returnFail(e.getMessage());
//        }
//
//    }

    @RequestMapping("loginOff")
    public Dto parentsLoginOff(HttpSession session){
        System.out.println("...家长退出");
        session.invalidate();

        return DtoUtil.returnSuccess("退出成功！");
    }

    @RequestMapping("queryStuScore")
    public Dto parentsMain(HttpServletRequest request, Integer page,Integer limit,HttpSession session,PageAssistant<StudentScoreVo> pa){
        System.out.println(">>>家长论坛>>>学生成绩");

        ParentsInfoVo parentsLoginInfo = (ParentsInfoVo) request.getSession().getAttribute("parentsLoginInfo");

        pa.setCurrPage(page);
        pa.setPageSize(limit);

        Long studentId = parentsLoginInfo.getStudentId();

        Map<String,Object> map=new HashMap<String,Object>();

        map.put("studentId",studentId);

        PageAssistant<StudentScoreVo> parentsStudentScore = studentScoreService.queryByPage(map, pa);

        return  DtoUtil.returnSuccess(parentsStudentScore);


//        try {
//            List<StudentScoreVo> studentScoreVoList =
//                    studentScoreService.query(new StudentScore(parentsLoginInfo.getStudentId()));
//
//            System.out.println(studentScoreVoList.size());
//            return DtoUtil.returnSuccess(studentScoreVoList);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            return DtoUtil.returnFail(e.getMessage());
//
//        }
    }

    @RequestMapping("personal")
    public Dto parentsPersonal(HttpServletRequest request, HttpSession session, PageAssistant<ParentsInfoVo> pa){
        System.out.println(">>>家长个人中心");

        ParentsInfoVo parentsLoginInfo =(ParentsInfoVo) request.getSession().getAttribute("parentsLoginInfo");

        System.out.println(parentsLoginInfo);

        List<ParentsInfoVo> list=new ArrayList<ParentsInfoVo>();
        list.add(parentsLoginInfo);
        pa.setItems(list);
        pa.setCount(list.size());

        return DtoUtil.returnSuccess(pa);
    }


    @RequestMapping("myChild")
    public Dto myChild(HttpServletRequest request, HttpSession session,  PageAssistant<StudentInfoVo> pa){
        System.out.println(">>>家长论坛>>>学生信息");

        ParentsInfoVo parentsLoginInfo =(ParentsInfoVo) request.getSession().getAttribute("parentsLoginInfo");

        System.out.println(parentsLoginInfo);

        try {

            List<StudentInfoVo> studentInfoVo = studentInfoService.query(new StudentInfo(parentsLoginInfo.getStudentId()));

//            List<StudentInfoVo> list=new ArrayList<StudentInfoVo>();
//            list.add(studentInfoVo);
            for (int i = 0; i <studentInfoVo.size() ; i++) {
                if (studentInfoVo.get(i).getAddress()!=null){
                    studentInfoVo.get(i).setAddress(transAddressService.transAddress(studentInfoVo.get(i).getAddress()));
                }
                if (studentInfoVo.get(i).getBornplace()!=null){
                    studentInfoVo.get(i).setBornplace(transAddressService.transAddress(studentInfoVo.get(i).getBornplace()));
                }
                if (studentInfoVo.get(i).getNativePlace()!=null){
                    studentInfoVo.get(i).setNativePlace(transAddressService.transAddress(studentInfoVo.get(i).getNativePlace()));
                }
            }
            pa.setItems(studentInfoVo);

            return DtoUtil.returnSuccess(pa);

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage());

        }
    }

    @RequestMapping("health")
    public Dto health(HttpServletRequest request, HttpSession session,  PageAssistant<PhysicalHealthVo> pa){
        System.out.println(">>>家长论坛>>>体质健康");

        ParentsInfoVo parentsLoginInfo =(ParentsInfoVo) request.getSession().getAttribute("parentsLoginInfo");

        System.out.println(parentsLoginInfo);

        try {

            List<PhysicalHealthVo> physicalHealthVo = physicalHealthService.query(new PhysicalHealth(parentsLoginInfo.getStudentId()));

//            List<PhysicalHealthVo> list=new ArrayList<PhysicalHealthVo>();
//            list.add(physicalHealthVo);
            pa.setItems(physicalHealthVo);
            pa.setCount(physicalHealthVo.size());

            return DtoUtil.returnSuccess(pa);

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage());

        }
    }

    @RequestMapping("communication")
    public Dto communication(HttpServletRequest request, HttpSession session, Integer page,Integer limit, PageAssistant<StudentCommunicationVo> pa ) {
        System.out.println(">>>家长论坛>>>交流情况");

        ParentsInfoVo parentsLoginInfo = (ParentsInfoVo) request.getSession().getAttribute("parentsLoginInfo");

        pa.setCurrPage(page);
        pa.setPageSize(limit);

        Long studentId = parentsLoginInfo.getStudentId();

        Map<String,Object> map=new HashMap<String,Object>();

        map.put("studentId",studentId);

        PageAssistant<StudentCommunicationVo> studentCommunication = studentCommunicationService.queryByPage(map, pa);

        return  DtoUtil.returnSuccess(studentCommunication);

    }


}
